<?php
class Page_Be_Products_GroupStyles extends Page {
	protected $tableObject;

	function __construct() {
		self::$TPL->appendJsFile('admin/group_styles');
		$this->idTableObject = get('id', 'int');
		if (!$this->idTableObject) {
			$this->idTableObject = post('styleId', 'int');
		}
		$this->actionsTableObject = array(
			'ajaxEditAction'
		);
		$this->classTableObject = 'Db_Products_GroupStyles';
		parent::__construct();
	}

	public function ajaxEditAction(){
		$value = post('value');
		$oldValue = $this->tableObject->gs_name;
		if($oldValue != $value){
			$this->tableObject->gs_name = $value;
			if(!$this->tableObject->save(true) ){
				$value = $oldValue;
			}
		}
		return array(
			'type' => 'string',
			'content' => ct(T_GROUP_STYLES_TITLE, $value) . '<input type="hidden" class="translation_key" value="' . $value . '">'
		);
	}

	public function deleteAction(){
		perm('PRODUCT_STYLES_EDIT', '/products/styles');
		$id = get('id', 'int');
		if ($id && (int) $id > 0 && Db_Products_GroupStyles::deleteByField('id', $id)) {
			$this->message= t(T_FORM_MSG, 'success');
		} else {
			$this->error = true;
			$this->message = t(T_FORM_MSG, 'unknown_error_occurred');
		}
		return array(
			'type' => 'redirect',
			'query' => false
		);
	}

	public function ajaxSaveAction(){
		perm('PRODUCT_STYLES_EDIT', '/products/styles');
		$styles = array_filter(post('styles', 'array'), function($item){
			return is_array($item) && 1 == count($item) && isset($item['gs_name']) && strlen(trim($item['gs_name']));
		});
		if (count($styles) && Db_Products_GroupStyles::insertBatch($styles)) {
			$this->message= t(T_FORM_MSG, 'success');
		} else {
			$this->error = true;
			$this->message = t(T_FORM_MSG, 'unknown_error_occurred');
		}
		return array(
			'type' => 'redirect',
			'query' => false
		);
	}

	public function defaultAction(){
		if (perm('PRODUCT_STYLES_EDIT')) {
			$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '#add');
		}
		self::$TPL->setBodyTemplate('list_group_styles');
		return array(
			'type' => 'display'
		);
	}

	public function ajaxLoadStylesDataAction(){
		perm('PRODUCT_STYLES_VIEW', '/products/styles');
		$aaData = array();
		$allColumns = array('nr', 'gs_name', 'action');
		$searchColumns = array('gs_name');
		$totalFilteredElements = $totalElements = Db_Products_GroupStyles::getTotalElements();
		list($searchQuery,$orderBy) = Utils::getSqlQueriesForPagination($allColumns, $searchColumns);

		if($searchQuery !== false) {
			// Paging
			$totalFilteredElements = Db_Products_GroupStyles::getTotalElements($searchQuery);
			$limit = '';
			if (post('iDisplayLength', 'int') > 0) {
				$limit = "LIMIT ".post('iDisplayStart', 'int').', '.post('iDisplayLength', 'int');
			}

			$aaData = Db_Products_GroupStyles::loadAll($limit, $searchQuery, $orderBy);
			if ($aaData) {
				array_walk($aaData, function(&$subarray, $key){
					$id = $subarray['id'];
					if ($subarray['product_id']) {
						$action = '';
					} else {
						$action = '<a class="group-style-delete-button btn small icon i_minus red" onclick="confirmDelete(\'?action=delete&id='.$id.'\')">'.t(T_FORM_BUTTON, 'delete').'</a>';
					}
					$subarray = array($key + 1, '<div styleId="' . $id . '" translationKey="' . $subarray['gs_name'] . '">' . ct(T_GROUP_STYLES_TITLE, $subarray['gs_name']) . '</div>', $action);
				});
			}
		}

		$data = array(
			'aaData' => $aaData,
			'sEcho' => post('sEcho', 'int'),
			'iTotalRecords' => $totalElements,
    		'iTotalDisplayRecords' => $totalFilteredElements,
		);
		return array(
			'type' => 'encoded_json',
			'content' => json_encode($data)
		);
	}
}